<?php
namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Session;
use think\Db;
use think\Cache;


class Index extends IndexBase
{
    public function index()
    {   
        $movie = '战狼2';
        $apikey = '8e00110b59abe1d91af094a7d8d41574';
        $url = 'http://op.juhe.cn/onebox/movie/video?dtype=json&key='.$apikey.'&q='.$movie;

        // CURL 初始化
        $curl = curl_init();

        // 配置curl
        curl_setopt($curl, CURLOPT_URL, $url);

        //  将curl_exec()获取的信息以文件流的形式返回，而不是直接输出。  
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

        // CURL执行
        $data = curl_exec($curl);

        // CURL 关闭
        curl_close($curl);
        $obj = json_decode($data);

        $result = $obj->result;

        $data = [
            'pic' => $result->cover,
            'title' => $result->title,
            'desc' => $result->desc
        ];

        return view('index@index/index',[
            'banner' => $this->banner(), 
            'category' => $this->category(),
            'hot' => $this->hot(), 
            'NewArr' => $this->NewArr(), 
            'NewArrs' => $this->NewArrs(),
            'Attr' => $this->Attr(),
            'movie' => $data
        ]);
    }
    /**
     * [banner 轮播图]
     * @return [type] [description]
     */
    public function banner()
    {
        return $banner = db('banner')->field('pic')->where('display',1)->cache('banner',3600)->select();
    }

    /**
     * [category 分类导航]
     * @return [type] [description]
     */
    public function category()
    {
        return $category = db('category')->where('pid = 0 and display = 1')->cache('category',3600)->select();
    }

    /**
     * [hot 热门版块]
     * @return [type] [description]
     */
    public function hot()
    {
        return $hot = db('yshop_show')->field('sid,icon,comment')->where('face = 1 and status = 1')->select();
    }

    /**
     * [NewArr 新品推荐1]
     */
    public function NewArr()
    {
        return $NewArr = db('newarr')->field('sid,menu,pic,desc')->where('status = 1')->paginate(4);
    }

    /**
     * [NewArrs 新品推荐2]
     */
    public function NewArrs()
    {
        return $NewArr = db('newarr')->field('sid,menu,pic,desc')->where('status = 2')->paginate(4);
    }

    /**
     * [cates 属性条件遍历]
     * @return [type] [description]
     */
    public function Attr()
    {
        return $Attr = db('attr')->field('id,attr')->where('status = 1')->paginate(10);
    }

    /**
     * [search 搜索查询]
     * @param  Request $request [搜索框请求数据]
     * @return [type]           [description]
     */
    public function search(Request $request)
    {
        $search = $request->get();
        $search = $search['word'];
        $yshop = Db::name('yshop')->field('id,name')->where('name','like',"%$search%")->where('status','eq','1')->select();

//       定义一个空数组
        $yshop_show = array();
        foreach($yshop as $v){
            $sid = $v['id'];
            $yshop_show[] = Db::name('yshop_show')->field('sid,comment,icon')->where(['face'=>1,'sid'=>$sid])->select();
        }

        return view('index@search/index',[
            'category' => $this->category(),
            'show'=>$yshop_show, 
            'Attr'=>$this->Attr()
        ]);
    }

    /**
     * [AttrSearch 多条件筛选搜索]
     */
    public function AttrSearch(Request $request)
    {
        $data = $request->get();

        $Attrs= Db::name('yshop_attr as a,lamp_yshop as b,lamp_yshop_show as c')
        ->field('a.sid,b.name,c.icon')
        ->where('a.sid = b.id and c.sid = b.id and c.face = 1')
        ->group('b.name')
        ->select();

        return view('index@search/Attrs',[
            'category' => $this->category(),
            'show'=>$Attrs, 
            'Attr'=>$this->Attr()
        ]);
    }

    /**
     * [AllShop AJAX滚动播放]
     */
    public function AllShop(Request $request)
    {
        //查看是不是ajax传送
        if(!Request::instance()->isAjax()){
           $this->error('非法请求!');
        }
        
        $data = Db::name('yshop')->field('name')->select();
        $result = array_rand($data,1);

        return json($data[$result]);
    }

    /**
     * [MovieApi 电影API接口]
     */
    public function MovieApi(Request $request)
    {
        $movieName = $request->get();
        
        $movie = urlencode($movieName['mname']);
        $apikey = '8e00110b59abe1d91af094a7d8d41574';
        $url = 'http://op.juhe.cn/onebox/movie/video?dtype=json&key='.$apikey.'&q='.$movie;

        // CURL 初始化
        $curl = curl_init();

        // 配置curl
        curl_setopt($curl, CURLOPT_URL, $url);

        //  将curl_exec()获取的信息以文件流的形式返回，而不是直接输出。  
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

        // CURL执行
        $data = curl_exec($curl);

        // CURL 关闭
        curl_close($curl);

        $obj = json_decode($data);

        $result = $obj->result;

        if (!$result) {
            $list['status'] = false;
            $list['info'] = '您搜索的电影不存在!';
            return json($list);
        }else{
             $list = [
                'status' => true,
                'pic' => $result->cover,
                'title' => $result->title,
                'desc' => $result->desc
            ];
            return json($list);
        }
           
    }

}

